<template>
    <el-dialog :title="dialogStatus | titleFilter" :close-on-click-modal="false" width="600px" :visible.sync="visible">
        <el-form ref="temp" :model="temp" :rules="rule" label-width="120px" @keyup.enter.native="dataFormSubmit()">

            <el-form-item label="银行名称" prop="bankName">
                <el-input v-model="temp.bankName" placeholder="请输入" />
            </el-form-item>

            <el-form-item label="银行账号" prop="accountNumber">
                <el-input v-model="temp.accountNumber" placeholder="请输入" />
            </el-form-item>

            <el-form-item label="开户行" prop="bankAddress">
                <el-input v-model="temp.bankAddress" placeholder="请输入" />
            </el-form-item>

            <el-form-item label="开户姓名" prop="accountName">
                <el-input v-model="temp.accountName" placeholder="请输入" />
            </el-form-item>

            <el-form-item label="备注" prop="remark">
                <el-input v-model="temp.remark" placeholder="备注" />
            </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">
                取消
            </el-button>
            <el-button type="primary" @click="dataFormSubmit">
                确认
            </el-button>
        </span>
    </el-dialog>
</template>
<script>
import { createAccount, updateAccount } from '@/api/assets'// 请求数据
export default {
    filters: {
        // 过滤弹框的标题
        titleFilter (type) {
            const textMap = {
                create: '新增',
                update: '修改'
            }
            return textMap[type]
        }
    },
    data () {
        return {
            visible: false,
            dialogStatus: '', // 弹框新增或者修改
            temp: { // 表单数据
                id: '',
                bankName: '',//银行名称
                accountNumber: '',//	行账号
                bankAddress: '',//开户行
                accountName: '',//开户姓名
                remark: ''
            },
            rule: {
                bankName: [
                    { required: true, message: '不能为空', trigger: 'blur' }
                ],
                accountNumber: [
                    { required: true, message: '不能为空', trigger: 'blur' }
                ],
                bankAddress: [
                    { required: true, message: '不能为空', trigger: 'blur' }
                ],
                accountName: [{ required: true, message: '不能为空', trigger: 'blur' }
                ],
            },

        }
    },
    created () {
    },
    methods: {
        // 初始化
        init (status, param) {
            this.dialogStatus = status // 标题
            this.visible = true // 显示
            this[status](param) // 新增或者修改
            this.$nextTick(() => { // 重置表单
                this.$refs['temp'].clearValidate()
            })
        },
        create () {
            this.resetTemp() // 清空表单
        },
        async update (param) {
            console.log(param)
            let data = Object.assign({}, param)//从上一页带过来当前的数据
            // const { data, code } = await getParkDetails({}, param.id) // 从后台获取当前的的数据

            // 成功
            this.temp.id = data.id
            this.temp.bankName = data.bankName
            this.temp.accountNumber = data.accountNumber
            this.temp.bankAddress = data.bankAddress
            this.temp.accountName = data.accountName
            this.temp.remark = data.remark

        },
        // 新增数据清空表单数据
        resetTemp () {
            this.temp = {
                id: '',
                bankName: '',//银行名称
                accountNumber: '',//	行账号
                bankAddress: '',//开户行
                accountName: '',//开户姓名
                remark: ''
            }
        },
        // 表单提交
        dataFormSubmit () {
            this.$refs['temp'].validate(async (valid) => {
                if (!valid) return
                const params = {
                    'id': this.temp.id || undefined,
                    'bankName': this.temp.bankName,//银行名称
                    'accountNumber': this.temp.accountNumber,//	行账号
                    'bankAddress': this.temp.bankAddress,//开户行
                    'accountName': this.temp.accountName,//开户姓名
                    'remark': this.temp.remark
                }
                const textMap = {
                    create: createAccount, // 新增
                    update: updateAccount // 修改
                }

                // 提交表单
                const { code, message } = await textMap[this.dialogStatus](params)

                // 成功
                this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                        this.visible = false
                        this.$emit('getList')
                    }
                })
            })
        },

        // ===>>>>>>>>>

        park_change (e) {
            console.log(e)
        },
        handleAvatarSuccess (res, file) {
            this.temp.idcardPicFront = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload (file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 8;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        }



    }
}
</script>

<style lang="scss">
.mod-menu {
    .menu-list__input,
    .icon-list__input {
        > .el-input__inner {
            cursor: pointer;
        }
    }
    &__icon-popover {
        width: 458px;
        overflow: hidden;
    }
    &__icon-inner {
        width: 478px;
        max-height: 258px;
        overflow-x: hidden;
        overflow-y: auto;
    }
    &__icon-list {
        width: 458px;
        padding: 0;
        margin: -8px 0 0 -8px;
        > .el-button {
            padding: 8px;
            margin: 8px 0 0 8px;
            > span {
                display: inline-block;
                vertical-align: middle;
                width: 18px;
                height: 18px;
                font-size: 18px;
            }
        }
    }
    .icon-list__tips {
        font-size: 18px;
        text-align: center;
        color: #e6a23c;
        cursor: pointer;
    }
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
}
.avatar {
    width: 120px;
    height: 120px;
    display: block;
}
</style>
